package tech.horgee.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import tech.horgee.common.IWrapper;
import tech.horgee.group.UpdateGroup;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 操作日志
 *
 * @author haojun
 * @since 2023-08-10
 */
@Getter
@Setter
@TableName("tab_oper_log")
public class OperLog implements Serializable, IWrapper<OperLog> {

    private static final long serialVersionUID = 1L;

    /**
     * 日志主键
     */
    @NotNull(groups = UpdateGroup.class)
    @TableId(value = "oper_id", type = IdType.AUTO)
    private Long operId;

    /**
     * 请求方式
     */
    private String requestMethod;

    /**
     * 请求URL
     */
    private String operUrl;

    /**
     * 类名
     */
    private String className;

    /**
     * 方法名称
     */
    private String method;

    /**
     * 请求参数
     */
    private String operParam;

    /**
     * 返回参数
     */
    private String jsonResult;

    /**
     * 操作状态（1正常 0异常）
     */
    private Boolean status;

    /**
     * 操作人员
     */
    private String operName;

    /**
     * 错误消息
     */
    private String errorMsg;

    /**
     * 操作时间
     */
    private LocalDateTime operTime;
    /**
     * 开始时间
     */
    @TableField(exist = false)
    private LocalDateTime startTime;
    /**
     * 结束时间
     */
    @TableField(exist = false)
    private LocalDateTime endTime;

    @Override
    public LambdaQueryWrapper<OperLog> wrapper() {
        return new LambdaQueryWrapper<OperLog>()
                .eq(Objects.nonNull(operId), OperLog::getOperId, operId)
                .eq(Objects.nonNull(requestMethod), OperLog::getRequestMethod, requestMethod)
                .eq(Objects.nonNull(className), OperLog::getClassName, className)
                .eq(Objects.nonNull(method), OperLog::getMethod, method)
                .eq(Objects.nonNull(operParam), OperLog::getOperParam, operParam)
                .eq(Objects.nonNull(jsonResult), OperLog::getJsonResult, jsonResult)
                .eq(Objects.nonNull(status), OperLog::getStatus, status)
                .like(Objects.nonNull(operName), OperLog::getOperName, operName)
                .eq(Objects.nonNull(errorMsg), OperLog::getErrorMsg, errorMsg)
                .eq(Objects.nonNull(operTime), OperLog::getOperTime, operTime)
                .ge(Objects.nonNull(startTime), OperLog::getOperTime, startTime)
                .le(Objects.nonNull(endTime), OperLog::getOperTime, endTime)
                .orderByDesc(OperLog::getOperId)
                ;
    }
}
